/*
 * @Author: ZhangZheyi
 * @Date:   2016-05-19 11:24:22
 * @Last Modified by:   ZhangZheyi
 * @Last Modified time: 2016-05-19 17:58:30
 */

'use strict';

var path = require('path');
var HtmlwebpackPlugin = require('html-webpack-plugin');  //自动快速的生成HTML
var glob = require('glob'); //返回和筛选文件夹下的所有文件 

var webpack = require('webpack');
var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common','common.js');


//定义了一些文件夹的路径
var ROOT_PATH = path.resolve(__dirname);
var PUBLIC_PATH = path.resolve(ROOT_PATH, 'public/javascripts');
var BUILD_PATH = path.resolve(ROOT_PATH, 'build/js');

function entries (globPath, ignores) {
    var files = glob.sync(globPath,{
        ignore: ignores
    });
    var entries = {}, entry, dirname, basename;

    for (var i = 0; i < files.length; i++) {
        entry = files[i];
        dirname = path.dirname(entry);
        basename = path.basename(entry, '.js');
        entries[path.join(dirname, basename).replace('public\\javascripts\\', '')] = './' + entry;
    }

    return entries;
}


module.exports = {

    //插件项
    plugins: [
          //mmonsPlugin
        //添加我们的插件 会自动生成一个html文件
        // new HtmlwebpackPlugin({
        //     title: 'Hello World app'
        // })
        new webpack.optimize.CommonsChunkPlugin('common','common.js'),
      
    ],

    //其它解决方案配置  (process.cwd()：返回运行当前脚本的工作目录的路径。)
    resolve: {

        //自动扩展文件后缀名，意味着我们require模块可以省略不写后缀名
        extensions: ['', '.js', '.json', '.scss'],

        //模块别名定义，方便后续直接引用别名，无须多写长长的地址 require('zepto') 即可
        alias: {
            zepto: path.join(process.cwd(), "public/javascripts/lib/zepto/zepto.js")
        }
    },

    //入口 可以直接用文件夹名称 默认会找index.js 也可以确定是哪个文件名字
    entry: (function(entries){
            entries['common'] = ['zepto'];
            return entries;
        })(entries('public/javascripts/**/*.js',['public/javascripts/common/**/*.js','public/javascripts/lib/**/*.js'])),
     // entry:{
     //     common: ['jquery', 'moment'],
     //     common2: ['jquery', 'moment']
     // }
    //输出的文件名 合并以后的js会命名为bundle.js
    output: {
        path: BUILD_PATH,
        filename: '[name].bundle.js'
    }

};


//HtmlwebpackPlugin 自动快速的生成HTML
// plugins: [
//     new HtmlwebpackPlugin({
//       title: 'Hello World app'
//     })
// ]
